package br.com.webdol.session;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

import br.com.webdol.ejb.BaseSessionBean;
import br.com.webdol.model.Person;

@Stateless
@LocalBean
public class PersonSession extends BaseSessionBean{
	
	/**
	 * Serial version.
	 */
	private static final long serialVersionUID = -6216957278227871187L;

	public Person getPersonById(int personId) {
		return getSingleByPK(Person.class, personId);
	}
	
	public Person savePerson(Person person){
		getEm().persist(person);
		return person;
	}
	
	public List<Person> getAll(){
		return getList(Person.class, "select p from Person p");
	}
	
	@SuppressWarnings("unchecked")
	public List<Person> getPersonByName(String name){
//		return getList(Person.class, "select p from Person p where p.name like ?1", name);
//		Query query = getEm().createQuery("select p from Person p where p.name like :name", Person.class);
//		query.setParameter("name", name);
		Query query = getEm().createNamedQuery(Person.NMQ_GET_BY_NAME);
		query.setParameter("name", name);
		return query.getResultList();
	}
	
	public Person updatePerson(Person person) {
		return getEm().merge(person);
	}
	
	public void removePerson(Person person) {
		person = getEm().merge(person);
		getEm().remove(person);
	}
}
